// DicomControl.cpp : implementation file
//

#include "stdafx.h"
#include "ImageLab.h"
#include "DicomControl.h"
#include "ChildView.h"


// CDicomControl dialog

IMPLEMENT_DYNAMIC(CDicomControl, CDialog)

CDicomControl::CDicomControl(CWnd* pParent /*=NULL*/)
	: CDialog(CDicomControl::IDD, pParent)
{
	parent = pParent;
	::AfxInitRichEdit2();
	CDialog::Create(CDicomControl::IDD, parent);
}

CDicomControl::~CDicomControl()
{
}

void CDicomControl::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CDicomControl, CDialog)
	ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_DICOM_IMAGESLIDER, &CDicomControl::OnNMLDownDicomImageslider)
	ON_BN_CLICKED(IDC_DICOM_PLAY, &CDicomControl::OnBnClickedButton1)
END_MESSAGE_MAP()


// CDicomControl message handlers

//Set the list of dicom images
void CDicomControl::SetFileList(std::vector<string> newfilelist)
{
	filelist = newfilelist;
	listofimage.resize(filelist.size());
	for(unsigned int i=0; i<(unsigned int)filelist.size(); i++)
	{
		((CChildView*)parent)->OpenDICOMFile(CString(filelist[i].c_str()));
		listofimage[i].Load(CString("c:\\temp.bmp"));
	}
	return;
}

void CDicomControl::OnOK()
{
	return;
}

void CDicomControl::OnNMLDownDicomImageslider(NMHDR *pNMHDR, LRESULT *pResult)
{
	CSliderCtrl* slider = (CSliderCtrl*)CDicomControl::GetDlgItem(IDC_DICOM_IMAGESLIDER);
	slider->SetRange((int)0, (int)filelist.size()-1, 1);
	slider->SetPageSize((int)filelist.size()/10);
	int currentselection = slider->GetPos();
	((CChildView*)parent)->OpenDICOMFile(CString(filelist[currentselection].c_str()));
	*pResult = 0;
}

void CDicomControl::OnBnClickedButton1()
{
	//Get the frames to be played
	CEdit* edit = (CEdit*)CDicomControl::GetDlgItem(IDC_DICOM_PERFRAMES);
	CSliderCtrl* slider = (CSliderCtrl*)CDicomControl::GetDlgItem(IDC_DICOM_IMAGESLIDER);
	slider->SetRange((int)0, (int)filelist.size()-1, 1);
	CString cstring;
	edit->GetWindowTextW(cstring);
	CT2CA ascii(cstring);
	string perframestring(ascii);
	unsigned int perframes = atoi(perframestring.c_str());
	if(perframes<=0)
		perframes = 1;
	
	//Play the set
	for(unsigned int i=0; i<filelist.size(); i+=perframes)
	{
		//Open the image
		listofimage[i].Save(CString("c:\\temp.bmp"));
		((CChildView*)parent)->OnImageOpen(_T("c:\\temp.bmp"), _T("DICOM"));
		slider->SetPos((int)i);
	}
	return;
}
